package action;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSON;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.stereotype.Controller;

import service.IGoodsService;
import service.ISaleService;
import util.Result;
import vo.GoodsInfo;
import vo.SellMainInfo;
import vo.SellSubInfo;
import vo.VipInfo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Action(value = "saleAction")
public class SaleAction extends BaseAction implements ModelDriven<SellSubInfo> {
	protected ISaleService SaleService;
	protected SellSubInfo sellSubInfo;
	protected SellMainInfo sellMainInfo;
	protected IGoodsService goodsService;
	protected int goodsid;
	private int rows = 1;// 每页显示的记录数
	private int page = 20;// 当前第几页
	private double total = 0;
	private int id;
	private double pay;
	private String saleOrderNo;

	public void insertOrder() {
		GoodsInfo good = goodsService.findGoodsById(goodsid);
		SellMainInfo maininfo = SaleService
				.findMainOrderBySaleOrderId(saleOrderNo);
		String orderNo = maininfo.getSaleOrderNo();
		if (good != null) {
			if (SaleService.checkSimilar(goodsid, orderNo)) {
				SellSubInfo order = new SellSubInfo();
				order.setGoodsInfo(good);
				order.setAmount(1);
				order.setAccount(good.getPrice());
				order.setPrice(good.getPrice());
				order.setSellMainInfo(maininfo);
				SaleService.saveSubInfo(order);
			} else {
				SellSubInfo sub = SaleService.findSubOrderBygoods(goodsid,
						orderNo);
				int num = sub.getAmount() + 1;
				sub.setAmount(num);
				double account = num * Double.parseDouble(good.getPrice());
				sub.setAccount(Double.toString(account));
				SaleService.updateSubInfo(sub);
				this.getSaleOrder();

			}
		} else {
		}
	}

	public void getSaleOrder() {
		int total = SaleService.getTotalNum();
		List<SellSubInfo> list = SaleService.getSubOrder(saleOrderNo);
		try {
			writeJson(total, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 获得流水号
	public String newSaleNo() {
		StringBuilder sb = new StringBuilder();
		Calendar c = Calendar.getInstance();
		sb.append("SO");
		sb.append(c.get(Calendar.HOUR_OF_DAY));
		sb.append(Calendar.MINUTE);
		sb.append(Calendar.SECOND);
		Random r = new Random();
		for (int i = 0; i < 3; i++) {
			sb.append(r.nextInt(10));
		}
		return sb.toString();
	}

	public void newOrder() {
		SellMainInfo newOrder = new SellMainInfo();
		String orderNO = this.newSaleNo();
		newOrder.setSaleOrderNo(orderNO);
		newOrder.setSelldate(new Timestamp(System.currentTimeMillis()));
		SaleService.saveMainOrder(newOrder);
		this.writeJson(newOrder);

	}

	public void loadsum() {
		double total = this.getSum(saleOrderNo);
		writeJson(total);
	}

	public void sum() {
		double total = this.getSum(saleOrderNo);
		SellMainInfo maininfo = SaleService
				.findMainOrderBySaleOrderId(saleOrderNo);
		maininfo.setTotal(Double.toString(total));
		SaleService.updateMainInfo(maininfo);
		SaleService.clearMainInfo();
	}

	public double getSum(String saleOrderNo) {
		List suborder = SaleService.getSubOrder(saleOrderNo);
		if (suborder != null) {
			Iterator it = suborder.iterator();
			while (it.hasNext()) {
				SellSubInfo subinfo = (SellSubInfo) it.next();
				double price = Double.parseDouble(subinfo.getGoodsInfo()
						.getPrice());
				double amount = subinfo.getAmount();
				total = total + price * amount;
			}
		}
		return total;
	}

	public void payment() {
		double total = this.getSum(saleOrderNo);
		double change = pay - total;
		if (change < 0) {
			this.writeJson(change);
		} else {
			this.writeJson(change);
		}

	}

	public void delete() {
		Result result = new Result();
		try {
			SellSubInfo subinfo = SaleService.findSubOrderBySaleOrderId(id);
			SaleService.deleteSub(subinfo);
			result.setMsg("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("删除失败");
		}
		writeJson(result);

	}

	public ISaleService getSaleService() {
		return SaleService;
	}

	public void setSaleService(ISaleService saleService) {
		SaleService = saleService;
	}

	@Override
	public SellSubInfo getModel() {
		// TODO Auto-generated method stub
		return sellSubInfo;
	}

	public SellSubInfo getSellSubInfo() {
		return sellSubInfo;
	}

	public void setSellSubInfo(SellSubInfo sellSubInfo) {
		this.sellSubInfo = sellSubInfo;
	}

	public SellMainInfo getSellMainInfo() {
		return sellMainInfo;
	}

	public void setSellMainInfo(SellMainInfo sellMainInfo) {
		this.sellMainInfo = sellMainInfo;
	}

	public int getGoodsid() {
		return goodsid;
	}

	public void setGoodsid(int goodsid) {
		this.goodsid = goodsid;
	}

	public IGoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsServices(IGoodsService goodsService) {
		// TODO Auto-generated method stub
		this.goodsService = goodsService;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getPay() {
		return pay;
	}

	public void setPay(double pay) {
		this.pay = pay;
	}

	public String getSaleOrderNo() {
		return saleOrderNo;
	}

	public void setSaleOrderNo(String saleOrderNo) {
		this.saleOrderNo = saleOrderNo;
	}

}
